#!/bin/bash
# File Name: monitor_pt.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-02-22 18:40:11
# Function: 检查pt工具执行进度
#########################################################################
:<<comment
变量注释:
db_name       : 业务库的库名，一般为'mafengwo'，要根据实际情况配置;
mysql_user    : 连接mysql用户名;
mysql_passwd  : 连接mysql的密码;
log_file      : 日志文件;
mysql_host    : 节点的ip地址
mysql_port    : mysql端口号
comment
db_name="mafengwo"
mysql_user="xxxxxxx"
mysql_passwd="xxxxxxxx"
mysql_host="192.168.xx.xxx"
mysql_port="3306"
start_time="2019-02-15 10:09:00"
type_list=(all finish excuting waiting)
type="${1}"
[ "${type}x" == "x" ] && type="${type_list[0]}"
 
#以下无需编辑修改
mysql_comm="mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_host} -P${mysql_port} ${db_name}"
start_time_seconds=$(date -d "${start_time}" +%s)
end_time="$( date -d"$(date "+%F %H:%M:%S")" +%s)"
format_time=""
[ ! -s "table.log" ] && exit || table_list=($(cat table.log))
max_table_len=0
if [ "${1}x" == "x" ]
then
    echo -en "\033[32mshell > bash ${0} [OPTION]\033[0m"
    echo -e "\033[33m"
    echo "-----------------------------"
    echo "参数介绍如下:"
    echo "${type_list[0]}     显示所有表"
    echo "${type_list[1]}  显示已经完成的表"
    echo "${type_list[2]}  显示正在执行的表"
    echo "${type_list[3]}    显示还没开始执行的表"
    echo "-----------------------------"
    echo "shell > bash ${0} ${type_list[0]}"
    echo -e "\033[0m"
    PS3="Enter a number [1-4]: "
    select type in "${type_list[0]}" "${type_list[1]}" "${type_list[2]}" "${type_list[3]}"
    do
        if [ "$(echo "${type_list[@]}"|grep -c ${type} 2>/dev/null)x" == "1x" ]
        then
            break
        else
            echo -n
            continue
        fi
    done
fi
for ((i=0;i<${#table_list[@]};i++))
do
    table=$(awk -F: '{print $3}' <<< "${table_list[${i}]}")
    [ ${#table} -gt ${max_table_len} ] && max_table_len=$((${#table}+1))
done

function f_format_time()
{
    time_tmp=${1}
    format_time="$(awk '$1<60{$1=$1" s ";print $0;next}$1>=60&&$1<3600{$1=int($1/60)" m "$2%60" s ";print $0;next}$1>=3600&&$1<86400{$1=int($1/3600)" h "int($1%3600/60)" m "$1%60" s ";print $0;next}$1>86400{$1=int($1/86400)" d "int($1%86400/3600)" h "int($1%3600/60)" m "$1%60" s ";print $0}' <<< "${time_tmp}")"
}

function f_format_print_info()
{
    show_str=${1}
    max_len=${2}
    tmp_len=$((${max_len}-${#show_str}))
    for ((var=0;var<${tmp_len};var++))
    do
        if [ "${1}x" == "-x" ]
        then
            show_str="${show_str}-"
        else
            show_str="${show_str} "
        fi
    done
}

function f_enter_str()
{
    str_type1="${1}"
    str_type2="${2}"
    str_type3="${3}"
    str_type4="${4}"
    str_type5="${5}"
    f_format_print_info "${str_type1}" "${max_table_len}"
    clo1=${show_str}
    f_format_print_info "${str_type2}" "8"
    clo2=${show_str}
    f_format_print_info "${str_type3}" "19"
    clo3=${show_str}
    f_format_print_info "${str_type4}" "19"
    clo4=${show_str}
    f_format_print_info "${str_type5}" "19"
    clo5=${show_str}
    if [ "${str_type1}x" == "-x" ]
    then
        echo -e "\033[32m+${clo1}-+-${clo2}-+-${clo3}-+-${clo4}-+-${clo5}+\033[0m"
    else
        echo -e "\033[32m|\033[0m\033[33m${clo1}\033[0m \033[32m|\033[0m \033[33m${clo2}\033[0m \033[32m|\033[0m \033[33m${clo3}\033[0m \033[32m|\033[0m \033[33m${clo4}\033[0m \033[32m|\033[0m \033[33m${clo5}\033[0m\033[32m|\033[0m"
    fi
}

f_enter_str "-" "-" "-" "-" "-"
f_enter_str " table name" "schedule" "starting time" "used time" "remaining time"
f_enter_str "-" "-" "-" "-" "-"

for table_info in ${table_list[@]}
do
    mysql_host=$(awk -F: '{print $1}' <<< "${table_info}")
    mysql_port=$(awk -F: '{print $2}' <<< "${table_info}")
    table=$(awk -F: '{print $3}' <<< "${table_info}")
    mysql_comm="mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_host} -P${mysql_port} ${db_name}"
    start_time="$(${mysql_comm} -NBe "SELECT create_time FROM  information_schema.TABLES where table_name='_${table}_new' and table_schema='${db_name}';")"
    start_time_seconds=$(date -d "${start_time}" +%s)
    count_time=$((${end_time}-${start_time_seconds}))
    if [ "${type}x" == "${type_list[3]}x" -o "${type}" == "${type_list[1]}x" ]
    then
        tmp_table=$(${mysql_comm} -NBe "show tables"|grep -c "^_${table}_new$")
        if [ "${tmp_table}x" != "0x" ]
        then
            continue
        fi
    elif [ "${type}x" == "${type_list[0]}x" -o "${type}x" == "${type_list[2]}x" ]
    then
        jindu=$(${mysql_comm} -NBe "select (select count(*) from _${table}_new)/(select count(*) from ${table})" 2>/dev/null)
    fi
    if [ "${jindu}x" == "x" ]
    then
        if [ "${type}x" == "${type_list[2]}x" ]
        then
            continue
        fi
        pri=$(${mysql_comm} -NBe "show create table \`${table}\`\\G" 2> /dev/null|grep -c "PRIMARY KEY")
        if [ "${pri}x" == "0x" ]
        then
            if [ "${type}x" == "${type_list[1]}x" ]
            then
                continue
            fi
            schedule="0.00%"
            used_time="0 s"
            remaining_time="UNKONW"
        else
            if [ "${type}x" == "${type_list[3]}x" ]
            then
                continue
            fi
            end_time_tmp="$(${mysql_comm} -NBe "SELECT create_time FROM  information_schema.TABLES where table_name='_${table}_old' and table_schema='${db_name}';")"
            if [ "${end_time_tmp}x" != "x" ]
            then
                end_time_tmp="$( date -d"${end_time_tmp}" +%s)"
                count_time=$((${end_time_tmp}-${start_time_seconds}))
                f_format_time "${count_time}"
                used_time=${format_time}
            else
                used_time="UNKONW"
            fi
            remaining_time="0 s"
            schedule="100.00%"
        fi
        starting_time="${start_time}"
        f_enter_str " ${table}" "${schedule}" "${starting_time}" "${used_time}" "${remaining_time}"
        f_enter_str "-" "-" "-" "-" "-"
        continue
    fi
    need_time_tmp=$(awk '{print sprintf("%.0f",(1-$1)/$1*$2)}' <<< "${jindu} ${count_time}")
    f_format_time "${need_time_tmp}"
    need_time=${format_time}
    f_format_time "${count_time}"
    count_time=${format_time}
    schedule="$(awk '{print sprintf("%.2f",$1*100,2)"%"}' <<< "${jindu}")"
    used_time="${count_time}"
    starting_time="${start_time}"
    remaining_time="${need_time}"
    f_enter_str " ${table}" "${schedule}" "${starting_time}" "${used_time}" "${remaining_time}"
    f_enter_str "-" "-" "-" "-" "-"
done
